<?php

namespace MyBlog\Http\Controllers\backend;

use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;
use MyBlog\Http\Requests;
use MyBlog\Models\Article;
use MyBlog\Models\Category;

class ArticleController extends BaseController
{
    public function index() {
        $articles = Article::where('id', '>', '0');
        if(!empty(Input::get('title'))) {
        	$articles = $articles->where('title', 'like', '%'.Input::get('title').'%');
        }
        if(!empty(Input::get('categoryId'))) {
        	$articles = $articles->where('category_id', Input::get('categoryId'));
        }
        if(!is_null(Input::get('disabled'))) {
        	$articles = $articles->where('disabled', Input::get('disabled'));
        }
        $articles = $articles->paginate(30);
        $categories = Category::where('disabled', false)->where('type', Category::TYPE_ARTICLES)->lists('name', 'id')->all();
        return view('backend.articles', compact('articles', 'categories'));
    }

    public function create() {
        $article = new Article();
        $categories = Category::where('disabled', false)->where('type', Category::TYPE_ARTICLES)->lists('name', 'id')->all();
        return view('backend.article_form', compact('article', 'categories'));
    }

    public function store() {
        $input = Input::all();
        dd(Input::get('tags'));
        $validation = Validator::make($input, Article::$rules, Article::$msgs);
        if($validation->fails()) {
            return Redirect::back()
                ->withInput()
                ->withErrors($validation);
        }
        if(!is_null($input['img_path'])) {
        	$input['img_path'] = QiniuUtils::upload($input['img_path']);
        }else {
        	unset($input['img_path']);
        }
        Article::create($input);
        return redirect('backend/articles');
    }
    public function edit(Article $article) {
        $categories = Category::where('disabled', false)->where('type', Category::TYPE_ARTICLES)->lists('name', 'id')->all();
        return view('backend.article_form', compact('article', 'categories'));
    }

    public function update(Article $article) {
        $input = Input::all();
        $validation = Validator::make($input, Article::$rules, Article::$msgs);
        if($validation->fails()) {
            return Redirect::back()
                ->withInput()
                ->withErrors($validation);
        }
        if(!is_null($input['img_path'])) {
        	$input['img_path'] = QiniuUtils::upload($input['img_path']);
        }else {
        	unset($input['img_path']);
        }
        $article->update($input);
        return Redirect::to('backend/articles');
    }

    public function destroy(Article $article) {
    	$article->delete();
    	return redirect('backend/articles');
    }

    public function changeState(Article $article) {
		$disabled = Input::get('disabled');
		$article->disabled = $disabled;
		$article->save();
		
		return redirect('backend/articles');
    }
}
